package com.javafactroy.kunlong.base.config.datasource;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.util.List;

@Data
@Slf4j
public class DatasourceConfig {
    protected String dbName;
    protected String dbType;
    protected String userName;

    protected void parse(DataSource dataSource) {

        DruidDataSource dds = (DruidDataSource) dataSource;

        String url = dds.getUrl();//ReflectUtil.getFieldValue(dataSource, "jdbcUrl");

        List<String> str = StrUtil.split(url, "?");
        int index = str.get(0).lastIndexOf("/");
        dbName = str.get(0).substring(index + 1);
        userName = dds.getUsername();
        dbType = dds.getDriverClassName();//.getFieldValue(dataSource, "driverClass").toString();
        if(dbType.contains("mysql")){
            dbType ="mysql";
        }
        if(dbType.contains("postgres")){
            dbType="postgres";
        }
        log.info("ds={}", this);
    }

}
